package br.com.cefetrn.apoena.converter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class Converter4Date extends AxConverterAPOENA {

	public static final DateFormat pt_BR_simple = new SimpleDateFormat(
			"dd/MM/yyyy");

	@Override
	public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
		try {
			String[] d = (String[]) arg1;
			LOG.debug("parse" + arg2.getClass());
			LOG.debug(d[0]);
			if (d[0] == null || d[0].equals(""))
				return null;
			Date r = pt_BR_simple.parse(d[0]);
			LOG.debug("" + r);
			return r;
		} catch (ParseException e) {
			LOG.error("", e);
			return null;
		}
	}

	@Override
	public String convertToString(Map arg0, Object arg1) {
		try {
			LOG.debug(this.getClass() + "string " + arg1);
			return toString((Date) arg1);
		} catch (Exception e) {
			return "erro";
		}
	}

	public String toString(Date arg1) {
		String r = "";
		try {
			r = pt_BR_simple.format(arg1);
		} catch (Exception e) {

		}
		return r;

	}

}
